package cn.mw.cmdb.service.impl;

import cn.mw.cmdb.entity.LayoutItemMapperInfo;
import cn.mw.cmdb.mongoMapper.LayoutItemDataMapper;
import cn.mw.cmdb.util.CMDBTool;
import cn.mw.components.mongodb.tool.CriteriaWrapper;
import cn.mw.microMonitorCommon.util.ModuleIDManager;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * 布局数据item信息映射接口
 */
public class LayoutItemMapperServiceImpl extends ModelBaseServiceImpl<LayoutItemMapperInfo, Object> {

    private CmdbServiceManage cmdbServiceManage;
    private LayoutItemDataMapper layoutItemDataMapper;
    public ModuleIDManager moduleIDManager;

    public static final String ID = "layoutItemMapper";

    public LayoutItemMapperServiceImpl(LayoutItemDataMapper layoutItemDataMapper, CmdbServiceManage cmdbServiceManage,
                                       ModuleIDManager moduleIDManager) {
        this.layoutItemDataMapper = layoutItemDataMapper;
        this.cmdbServiceManage = cmdbServiceManage;
        this.moduleIDManager = moduleIDManager;
    }

    @Override
    public LayoutItemMapperInfo genObject() {
        return new LayoutItemMapperInfo();
    }

    @Override
    Object doAdd(LayoutItemMapperInfo param) throws Exception {
        Update update = new Update();
        update.set(CMDBTool.getFieldName(LayoutItemMapperInfo::getMapperData), param.getMapperData());
        Query query = new Query(CriteriaWrapper.where(LayoutItemMapperInfo::getId).is(param.getId()));
        //id存在就更新，不存在则新增
        layoutItemDataMapper.upsert(query, update);
        return true;
    }

    @Override
    boolean doUpdate(LayoutItemMapperInfo param) throws Exception {
        return false;
    }

    @Override
    boolean doRemove(LayoutItemMapperInfo param) throws Exception {
        CMDBTool.remove(layoutItemDataMapper, param.getId(), LayoutItemMapperInfo.class, false);
        return true;
    }

    @Override
    Object doBatchAdd(Object params) throws Exception {
        return false;
    }

    @Override
    boolean doBatchUpdate(Object params) {
        return false;
    }

    @Override
    LayoutItemMapperInfo doSelectById(Object id) throws Exception {
        LayoutItemMapperInfo layoutItemMapperInfo = layoutItemDataMapper.findById(id);
        return layoutItemMapperInfo;
    }

    @Override
    List<LayoutItemMapperInfo> doSelectList(Object param) throws Exception {
        return null;
    }

    @Override
    boolean doBatchRemove(Object params) throws Exception {
        return false;
    }

   public boolean removeAll() throws Exception {
        Query query = new Query();
        layoutItemDataMapper.remove(query);
        return true;
    }
}